<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
// use Crypt;
use Hash;
use DB;
class GoodsController extends Controller
{
    
    public function add()
    {
    	$title = '商品添加';

    	$data = DB::table('cate')
    	-> where('pid','=',0)
    	-> get();
  
    	return view('admin.goods.add',['title' => $title,'data' => $data]);	
    }
    //商品执行添加
    public function insert(Request $request)
    {

    	$data = $request -> except('_token');
    	// if(empty($data['username']) || empty($data['password']))
    	// {
    	// 	return back() -> with(['username' => '用户名不能为空','password' => '密码不能为空']);
    	// }
    	// 
    	// dd($data);

    	$this->validate($request, [
		    'goodsname' => 'required',
		    'description' => 'required',
		    'value' => 'required',
		    
		    'inventory' => 'required',
		    'cid' => 'required',
		    'logo' => 'image',
		    'logo' => 'required',
		],[
			'goodsname.required' => '商品名不能为空',
			'description.required' => '商品描述不能为空',
			'value.required' => '商品价格不能为空',
			
			'inventory.required' => '商品库存不能为空',
			'cid.required' => '商品种类不能为空',
			'logo.image' => '图片格式不对',
			'logo.required' => '图片不能为空',
			
		]);

		//处理pic字段
		if($request -> hasFile('logo'))
		{
			if($request -> file('logo') -> isValid())
			{
				//获取后缀名
				$suffix = $request -> file('logo') -> getClientOriginalExtension();
				// echo $suffix;
				//随机文件名
				$fileName = time().mt_rand(100000,999999).'.'.$suffix;
				// echo $fileName;
				$path = './goodsuploads';
				$request -> file('logo') -> move($path,$fileName);

				$data['logo'] = trim($path.'/'.$fileName,'.');
				// echo $data['pic'];
			}
		}
		
		$data['status'] = 1;
		$data['time'] = time();
		// dd($data);
		$res = DB::table('goods') -> insert($data);
		

		if($res)
		{
			return redirect('/admin/goods/index') -> with('success','添加成功');
		}else
		{
			return back() -> with('error','添加失败');
		}
		
    	
    }
    //商品列表页
    public function index(Request $request)
    {
    	$title = "这是商品列表";
    	//查询数据
    	
    	$data  = DB::table('goods as g')
    			->leftJoin('cate as c','c.id','=','g.cid')
    			->select('c.name','g.*')
    			-> orderBy('g.id')
    			->where(function($query) use ($request){
    			$query -> where('goodsname','like','%'.$request->input('keywords').'%');
    			}) -> paginate($request -> input('num',10));
    			
    
    	
    	return view('admin.goods.index',['title' => $title,'data' => $data,'request' => $request -> all()]);
    }



    //商品编辑
    public function edit($id)
    {
    	$title = '商品编辑';
    	//查询数据
    	$data = DB::table('goods') -> where('id','=',$id) -> first();

    	$data1 = DB::table('cate')
    	-> where('pid','=',0)
    	-> get();
    	// dd($data1);
    	return view('admin/goods/edit',['title' => $title,'data'=> $data,'data1'=>$data1]);
    }
    //商品更新
    //
    public function update(Request $request)
    {
    	// dd($request -> all());
    	$data = $request -> except('_token','oldpic');
    	$this->validate($request, [
		    'goodsname' => 'required',
		    'description' => 'required',
		    'value' => 'required',
		    'mark' => 'required',
		    'inventory' => 'required',
		    'cid' => 'required',
		    'logo' => 'image',
		],[
			'goodsname.required' => '商品名不能为空',
			'description.required' => '商品描述不能为空',
			'value.required' => '商品价格不能为空',
			'mark.required' => '商品货号不能为空',
			'inventory.required' => '商品库存不能为空',
			'cid.required' => '商品种类不能为空',
			'logo.image' => '图片格式不对',
			
		]);;

		// dd($data);
		//老图片
		$oldpath = '.'.$request -> input('oldpic');
		//处理pic字段
		if($request -> hasFile('logo'))
		{
			if($request -> file('logo') -> isValid())
			{
				//获取后缀名
				$suffix = $request -> file('logo') -> getClientOriginalExtension();
				// echo $suffix;
				//随机文件名
				$fileName = time().mt_rand(100000,999999).'.'.$suffix;
				// echo $fileName;
				$path = './goodsuploads';
				$request -> file('logo') -> move($path,$fileName);

				$data['logo'] = trim($path.'/'.$fileName,'.');
				// echo $data['pic'];
				 
				if(file_exists($oldpath))
				{
					unlink($oldpath);
				}

			}
		}
		// dd($data);
		//判断
		$res = DB::table('goods') -> where('id',$request->input('id')) -> update($data);
		if($res)
		{
			return redirect('/admin/goods/index') -> with(['success' => '更新成功']);
		}else{
			return back() -> with(['fail' => '更新失败']);
		}
    }

     //删除
    public function delete($id)
    {
    	$res = DB::table('goods') -> where('id',$id) ->delete();
    	if($res)
		{
			return redirect('/admin/goods/index') -> with(['success' => '删除成功']);
		}else{
			return back() -> with(['fail' => '删除失败']);
		}
    } 
    
}
